//
// Created by HP on 2024/2/1.
//

#include "mpu6050_driver.h"



MPU6050_Driver::MPU6050_Driver(GPIO_TypeDef* SCLPort,uint16_t SCLPin,GPIO_TypeDef* SDAPort,uint16_t SDAPin){
    iic.Init(SCLPort,SDAPort,SCLPin,SDAPin);
    iic.iic_write_data(MPU6050Addr,PWR_MGMT_1,0x80);
    HAL_Delay(100);
    iic.iic_write_data(MPU6050Addr,PWR_MGMT_1,0x02); //PLL，使用X轴陀螺作为参考
    iic.iic_write_data(MPU6050Addr,SMPLRT_DIV,0x09); //10ms 采集一次
    iic.iic_write_data(MPU6050Addr,MPU_CONFIG,0x03);
    iic.iic_write_data(MPU6050Addr,GYRO_CONFIG,0x18);// gyro scale  ：+-2000°/s
    iic.iic_write_data(MPU6050Addr,ACCEL_CONFIG,0x00);// Accel scale ：+-2g (65536/4=16384 LSB/g)
    HAL_Delay(50);

}

void MPU6050_Driver::Init(){
    iic.iic_write_data(MPU6050Addr,PWR_MGMT_1,0x80);
    HAL_Delay(100);
    iic.iic_write_data(MPU6050Addr,PWR_MGMT_1,0x02); //PLL，使用X轴陀螺作为参考
    iic.iic_write_data(MPU6050Addr,SMPLRT_DIV,0x09); //10ms 采集一次
    iic.iic_write_data(MPU6050Addr,MPU_CONFIG,0x03);
    iic.iic_write_data(MPU6050Addr,GYRO_CONFIG,0x18);// gyro scale  ：+-2000°/s
    iic.iic_write_data(MPU6050Addr,ACCEL_CONFIG,0x00);// Accel scale ：+-2g (65536/4=16384 LSB/g)
    HAL_Delay(50);
}

uint8_t MPU6050_Driver::read_ID(){


    char data = iic.iic_read_data(MPU6050Addr,MPU6050ID);


    return data;
}

uint8_t MPU6050_Driver::read(uint8_t addr){
    return iic.iic_read_data(MPU6050Addr,addr);
}

void MPU6050_Driver::write(uint8_t addr,uint8_t value){
    iic.iic_write_data(MPU6050Addr,addr,value);
}